# coding=utf-8
# 代码文件：chapter9/ch9.2.3.py
import os
# 从 PyPDF2模块导入PdfFileReader和PdfFileWriter类
from PyPDF2 import PdfFileReader, PdfFileWriter

#  设置输入目录
indir = r'data\in'
#  设置输出目录
outdir = r'data\out'

infile = os.path.join(indir, 'temp.pdf')

pdfReader = PdfFileReader(infile)

for page_no in range(pdfReader.numPages):
    page = pdfReader.getPage(page_no)  # 从原始PDF文档指定页面索引获得页面对象
    pdf_writer = PdfFileWriter()       # 创建PdfFileWriter对象
    pdf_writer.addPage(page)           # 添加页面到PdfFileWriter对象中

    file = '{0}.pdf'.format(page_no + 1)
    outfile = os.path.join(outdir, file)

    # 打开文件模式'wb'是表示写入二进制文件
    with open(outfile, 'wb') as output_pdf:
        # 通过PdfFileWriter对象将数据输出到文件中
        pdf_writer.write(output_pdf)

print('Game Over！')
